package controller;

import model.CommentModel;
import view.CommentView;

import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Comment {

    public CommentModel model;
    public CommentView view;
    
    public Comment(CommentModel mdl) {
        this.model = mdl;
        this.view = new CommentView(mdl);

        if (mdl.authorId.compareTo(App.getCurrentUser().userId) == 0)  {
            addContextMenu();
        }
    }

    public void addContextMenu() {
        JPopupMenu popup = new JPopupMenu();

        JMenuItem remove = new JMenuItem("Remove Comment");
        remove.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                model.remove();
                JComponent parent = (JComponent)view.getParent();
                parent.remove(view);
                parent.revalidate();
            }
        });

        popup.add(remove);

        this.view.setComponentPopupMenu(popup);

    }

}
